package com.itheima.admin.controller;

import com.itheima.admin.dto.ChannelPageRequestDto;
import com.itheima.admin.pojo.AdChannel;
import com.itheima.admin.service.AdChannelService;
import com.itheima.common.dto.PageRequestDto;
import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @version 1.0
 * @description 说明
 * @package com.itheima.admin.controller
 */
@RestController
@RequestMapping("/channel")
@Api(tags = "频道管理")
public class AdChannelController {

    @Autowired
    private AdChannelService adChannelService;

    /**
     * 查询频道列表
     * @param dto
     * @return
     */
    @PostMapping("/list")
    @ApiOperation("频道列表-分页查询")
    public PageResultVo list(@RequestBody ChannelPageRequestDto dto){
        PageResultVo vo = adChannelService.findPage(dto);
        return vo;
    }

    /**
     * 新增频道
     * @param adChannel
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("新增频道")
    public ResultVo add(@Validated @RequestBody AdChannel adChannel){
        adChannelService.add(adChannel);
        return ResultVo.ok("SUCCESS");
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("通过id查询")
    public ResultVo findById(@PathVariable int id){
        AdChannel adChannel = adChannelService.getById(id);
        return ResultVo.ok(adChannel);
    }

    /**
     * 更新频道
     * @param adChannel
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("更新频道")
    public ResultVo update(@RequestBody AdChannel adChannel){
        adChannelService.updateById(adChannel);
        return ResultVo.ok("SUCCESS");
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation("通过id删除")
    public ResultVo deleteById(@PathVariable int id){
        adChannelService.removeById(id);
        return ResultVo.ok("SUCCESS");
    }
}
